<?php
/**
 * 从user表同步查询数据
 */
apf_require_class('Oa_Job_Abstract');
apf_require_class('Bll_Hr_OutInterface');
class Oa_Job_Report_SyncUserData extends Oa_Job_Abstract {

    private $write_db = "hroa_master";//读数据库

    public function run($params) {
        $time_start = time();
        $this->log("开始执行啦！".date('Y-m-d H:i:s'));
        $this->hroa_bll = new Bll_Hr_OutInterface();
        if($params=='init'){
            $data = $this->_get_all_user_data();
            foreach ($data as $val){
                $this->_insert_data($val);
            }
        }else{
            $this->_sync_clear();
        }
        $time_end = time();
        $this->log('(End Job--'.date('Y-m-d H:i:s').')'.($time_end-$time_start).'seconds');

    }
    /**
     * 取离职人员的职位
     * @param unknown_type $uid
     */
    private function _get_user_work_info($uid){
        $sql = "SELECT * FROM work WHERE user_id={$uid} order by work_id desc limit 1 ";
        $tmp = $this->get_db_rows($sql,$this->write_db,array());
        $workdata = $tmp[0];
        $job_data = $this->hroa_bll->get_job_dep_info($workdata['job_id']);

        $indata['job_id']=$workdata['job_id'];
        $indata['position_name']=$job_data['position_name'];
        $indata['plan_num']=intval($val['job_plan_num']);
        $indata['fact_num']=intval($val['job_fact_num']);
        $indata['department_id']=intval($job_data['department_id']);
        $indata['department_name']=$job_data['department_name'];
        $indata['function_id']=intval($job_data['function_id']);
        $indata['bu_id']=intval($job_data['bu_id']);
        return $indata;


    }
    /**
     * 取所有在职的用户，如果不在dep_user就说明离职了，
     */
    private function _sync_clear(){
        $user_list = $this->_get_all_user_data();
        $sql = "SELECT * FROM dep_user WHERE 1 ";
        $sync_data = $this->get_db_rows_assoc($sql,$this->write_db,array());
        foreach($user_list as $val){
            $job_info = $this->_get_user_work_info($val['user_id']);
            $val = array_merge($val,$job_info);
            if(empty($sync_data[$val['user_id']])){
                $this->_insert_data($val,1,1);
            }else{
                if($val['job_status']==1 && $sync_data[$val['user_id']]['job_status']==1){
                    $this->_insert_data($val,2,1);//update
                }elseif($val['job_status']==2 && $sync_data[$val['user_id']]['job_status']==1){
                    //$sql = "update dep_user set last_date=".$val['last_date'].",leaving_date=".$val['leaving_date'].",leaving_type=".$val['leaving_type'].",job_status=2 WHERE user_id =".$val['user_id'];
                    //$this->execute($sql,array(),$this->write_db);
                    $this->_insert_data($val,2,0);//update
                }else{
                    //$sql = "update dep_user set last_date=".$val['last_date'].",leaving_date=".$val['leaving_date'].",leaving_type=".$val['leaving_type'].",job_status=".$val['job_status']." WHERE user_id =".$val['user_id'];
                    //$this->execute($sql,array(),$this->write_db);
                }
            }
        }
        return true;
    }
    private function _get_all_user_data($job_status=0){
        if($job_status){
            $where = "a.job_status=1";
        }else{
            $where = "1";
        }
        $result=array();
        $start = 0;
        $limit = 100;
        while(true) {
            //$sql="select a.user_id,a.user_name,a.user_code,a.enter_date,a.indue_date,a.leaving_date,a.last_date,a.user_type,a.job_status,a.leaving_type,a.p_level,a.m_level,a.degree,a.work_city_id,a.reason_id,a.other_reason,b.job_id,c.position_name,c.plan_num,c.fact_num,c.department_id,d.department_name,d.function_id,d.bu_id from user  as a LEFT JOIN work as b ON a.user_id=b.user_id  LEFT JOIN job as c ON c.job_id=b.job_id LEFT JOIN department as d ON  d.department_id=c.department_id where {$where} AND a.user_id >{$start} and b.is_deleted =1 order by a.user_id asc, b.work_id desc limit {$limit}";
            $sql="select a.user_id,a.user_name,a.user_code,a.enter_date,a.indue_date,a.leaving_date,a.last_date,a.user_type,a.job_status,a.leaving_type,a.p_level,a.m_level,a.degree,a.work_city_id,a.reason_id,a.other_reason from user as a where {$where} AND a.user_id >{$start} order by a.user_id asc limit {$limit}";
            $tmp = $this->get_db_rows($sql,$this->write_db,array());
            if(empty($tmp)) {
                $this->log("取完所有用户数据!");
                break;
            }
            $count = count($tmp);
            $start = $tmp[$count - 1]['user_id'];
            $result = array_merge($result,$tmp);
        }
        return $result;
    }
    private function _init(){
        $data = $this->_get_all_user_data();
        foreach ($data as $val){
            $this->_insert_data($val);
        }
    }
    /**
     * type=1 insert
     * type=2 delete
     * @param unknown_type $val
     * @param unknown_type $type
     */
    private function _insert_data($val,$type=1,$up_work=1){
        $indata=array(
                'user_id'=>$val['user_id'],
                'user_name'=>$val['user_name'],
                'user_code'=>$val['user_code'],
                'enter_date'=>$val['enter_date'],
                'indue_date'=>$val['indue_date'],
                'leaving_date'=>$val['leaving_date'],
                'last_date'=>$val['last_date'],
                'user_type'=>$val['user_type'],
                'reason_id'=>$val['reason_id'],
                'other_reason'=>$val['other_reason'],
                'job_status'=>empty($val['job_status']) ? 0 : intval($val['job_status']),
                'leaving_type'=>empty($val['leaving_type']) ? 0 : intval($val['leaving_type']),
                'p_level'=>$val['p_level'],
                'm_level'=>$val['m_level'],
                'degree'=>$val['degree']);
        if($indata['leaving_type']==3){
            $indata['last_date']=$indata['leaving_date']=$val['enter_date'];
        }
        if($up_work==1){
                $indata['work_city_id']=empty($val['work_city_id']) ? 0 : intval($val['work_city_id']);
                $indata['job_id']=empty($val['job_id']) ? 0 : intval($val['job_id']);
                $indata['position_name']=$val['position_name'];
                $indata['plan_num']=empty($val['plan_num']) ? 0 : intval($val['plan_num']);
                $indata['fact_num']=empty($val['fact_num']) ? 0 : intval($val['fact_num']);
                $indata['department_id']=empty($val['department_id']) ? 0 : intval($val['department_id']);
                $indata['department_name']=$val['department_name'];
                $indata['function_id']=empty($val['function_id']) ? 0 : intval($val['function_id']);
                $indata['bu_id']=empty($val['bu_id']) ? 0 : intval($val['bu_id']);
        }
        $sql = $this->_implode_field_value($indata);
        if($type==1){
            $sql = "INSERT INTO dep_user SET $sql";
        }else{
            $sql = "UPDATE dep_user SET $sql WHERE user_id={$val['user_id']}";
        }
        $this->execute($sql,array(),$this->write_db);
    }
    private function _implode_field_value($array, $glue = ',') {
        $sql = $comma = '';
        foreach ($array as $k => $v) {
            $sql .= $comma."`$k`='$v'";
            $comma = $glue;
        }
        return $sql;
    }
}
